package nl.maarten.docenthelp.entity.thread;

import java.io.IOException;
import java.net.DatagramPacket;

import nl.maarten.docenthelp.entity.Client;
import nl.maarten.docenthelp.entity.Session;

public class ListenThread  extends Thread
{
	private boolean running = false;
	
	private Session clientSession = Client.getSession();
	
	private byte[] receiveData = new byte[1024];
	
	private DatagramPacket packet;
	
	public void run()
	{
		running = true;
		
		while(running)
		{
			if(clientSession != null)
			{
				try
				{
					String line = clientSession.getIn().readLine();
					
					System.out.println( "Server: " + line );
				}
				catch( IOException e )
				{
					running = false;
					System.out.println( "Session closed!" );
				}
				
				try
				{
					packet = new DatagramPacket( receiveData, receiveData.length );
					clientSession.getUdpSocket().receive(packet);
					System.out.println( "Server: " + new String( packet.getData() ) );
				}
				catch( IOException e )
				{
					e.printStackTrace();
				}
			}
		}
	}
}
